package com.cf.apiInstance.component;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.cf.apiInstance.common.utils.DateUtils;
import com.cf.apiInstance.service.LogService;

import lombok.extern.slf4j.Slf4j;

/**
 * 系统任务调度
 * 
 * @author: cf
 * @date: 30 Nov 2018 11:19:59 am
 * @vesion: 1.0
 */
@Component
@Slf4j
public class SystemJobSchedule {

	@Resource
	private LogService logService;

	/**
	 * 每天凌晨执行任务
	 */
	private static final String EVERY_DAY_JOB_CRON = "01 00 00 * * ?";

	/** 日志保存时长，默认6个月 */
	@Value("${api.huashao.log_save_max_months:6}")
	private int log_save_max_months;

	@Scheduled(cron = EVERY_DAY_JOB_CRON)
	@Async
	public void execute() {
		log.info("凌晨定时任务开始执行：================================");
		long begin = System.currentTimeMillis();
		clearSystemLog();
		log.info("凌晨定时任务执行结束，耗时" + DateUtils.formatTime(begin, System.currentTimeMillis()) + "================================");
	}

	/**
	 * 清理历史日志 返回类型 void
	 */
	private void clearSystemLog() {
		Date time = DateUtils.getLastMonthStart(log_save_max_months);
		logService.clearLogsPassDay(time);
	}
}
